package shayu.order.listener;

import com.rabbitmq.client.Channel;
import io.renren.common.to.mq.SeckillOrderTo;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import shayu.order.service.OrderService;

import java.io.IOException;


@Component
@RabbitListener(queues = "order.seckill.order.queue")
public class OrderSeckillListener {

    @Autowired
    private OrderService orderService;

    @RabbitListener
    public void listener(SeckillOrderTo seckillOrderTo, Message message, Channel channel) throws IOException {
        try{
            orderService.createSeckillOrder(seckillOrderTo);
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
